// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.objects;

import zombie.Lua.LuaEventManager;
import zombie.core.Core;
import zombie.core.Rand;
import zombie.core.opengl.Shader;
import zombie.core.skinnedmodel.model.WorldItemModelDrawer;
import zombie.core.textures.ColorInfo;
import zombie.core.textures.Texture;
import zombie.inventory.InventoryItem;
import zombie.iso.IsoCell;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoPhysicsObject;

public class IsoFallingClothing extends IsoPhysicsObject {
    private InventoryItem clothing = null;
    private int dropTimer = 0;
    public boolean addWorldItem = true;

    @Override
    public String getObjectName() {
        return "FallingClothing";
    }

    public IsoFallingClothing(IsoCell cell) {
        super(cell);
    }

    public IsoFallingClothing(IsoCell cell, float x, float y, float z, float xvel, float yvel, InventoryItem _clothing) {
        super(cell);
        this.clothing = _clothing;
        this.dropTimer = 60;
        this.velX = xvel;
        this.velY = yvel;
        float float0 = (float)Rand.Next(4000) / 10000.0F;
        float float1 = (float)Rand.Next(4000) / 10000.0F;
        float0 -= 0.2F;
        float1 -= 0.2F;
        this.velX += float0;
        this.velY += float1;
        this.x = x;
        this.y = y;
        this.z = z;
        this.nx = x;
        this.ny = y;
        this.offsetX = 0.0F;
        this.offsetY = 0.0F;
        this.terminalVelocity = -0.02F;
        Texture texture = this.sprite.LoadFrameExplicit(_clothing.getTex().getName());
        if (texture != null) {
            this.sprite.Animate = false;
            int _int = Core.TileScale;
            this.sprite.def.scaleAspect((float)texture.getWidthOrig(), (float)texture.getHeightOrig(), (float)(16 * _int), (float)(16 * _int));
        }

        this.speedMod = 4.5F;
    }

    @Override
    public void collideGround() {
        this.drop();
    }

    @Override
    public void collideWall() {
        this.drop();
    }

    @Override
    public void update() {
        super.update();
        this.dropTimer--;
        if (this.dropTimer <= 0) {
            this.drop();
        }
    }

    @Override
    public void render(float x, float y, float z, ColorInfo info, boolean bDoAttached, boolean bWallLightingPass, Shader shader) {
        float _float = (float)(60 - this.dropTimer) / 60.0F * 360.0F;
        if (!WorldItemModelDrawer.renderMain(this.clothing, this.getCurrentSquare(), this.getX(), this.getY(), this.getZ(), _float)) {
            super.render(x, y, z, info, bDoAttached, bWallLightingPass, shader);
        }
    }

    void drop() {
        IsoGridSquare isoGridSquare = this.getCurrentSquare();
        if (isoGridSquare != null && this.clothing != null) {
            if (this.addWorldItem) {
                float _float = isoGridSquare.getApparentZ(this.getX() % 1.0F, this.getY() % 1.0F);
                isoGridSquare.AddWorldInventoryItem(this.clothing, this.getX() % 1.0F, this.getY() % 1.0F, _float - (float)isoGridSquare.getZ());
            }

            this.clothing = null;
            this.setDestroyed(true);
            isoGridSquare.getMovingObjects().remove(this);
            this.getCell().Remove(this);
            LuaEventManager.triggerEvent("OnContainerUpdate", isoGridSquare);
        }
    }

    void Trigger() {
    }
}
